package com.cycplus.test;

import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class PcmPlayer {

    private static final String TAG = "PcmPlayer";
    private static final int SAMPLE_RATE = 48000; // 采样率，PCM文件的采样率
    private static final int CHANNEL_CONFIG = AudioFormat.CHANNEL_OUT_STEREO; // 声道配置，根据PCM文件调整
    private static final int AUDIO_FORMAT = AudioFormat.ENCODING_PCM_16BIT; // 音频格式，根据PCM文件调整
    private static final int BUFFER_SIZE = AudioTrack.getMinBufferSize(SAMPLE_RATE, CHANNEL_CONFIG, AUDIO_FORMAT);

    private AudioTrack audioTrack;
    private FileInputStream fileInputStream;
    private Thread playThread;
    private byte[] buffer = new byte[BUFFER_SIZE];

    public void startPlaying(String filePath) {
        File pcmFile = new File(filePath);
        if (!pcmFile.exists()) {
            Log.e(TAG, "PCM file does not exist.");
            return;
        }

        audioTrack = new AudioTrack(
                AudioManager.STREAM_MUSIC,
                SAMPLE_RATE,
                CHANNEL_CONFIG,
                AUDIO_FORMAT,
                BUFFER_SIZE,
                AudioTrack.MODE_STREAM
        );

        try {
            fileInputStream = new FileInputStream(pcmFile);
            audioTrack.play();
            startPlayThread();
        } catch (IOException e) {
            Log.e(TAG, "Could not open PCM file for playback.", e);
        }
    }

    private void startPlayThread() {
        playThread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    int readSize;
                    while ((readSize = fileInputStream.read(buffer)) > 0 && !Thread.currentThread().isInterrupted()) {
                        int written = audioTrack.write(buffer, 0, readSize);
                        Log.d("zyq", "written = " + written);
                    }
                } catch (IOException e) {
                    Log.e(TAG, "Error occurred while playing PCM file.", e);
                } finally {
                    stopPlaying();
                }
            }
        }, "PCM Player Thread");

        playThread.start();
    }

    public void stopPlaying() {
        if (playThread != null) {
            playThread.interrupt();
            try {
                playThread.join();
            } catch (InterruptedException e) {
                Log.e(TAG, "Error stopping play thread.", e);
            }
            playThread = null;
        }

        if (audioTrack != null) {
            audioTrack.stop();
            audioTrack.release();
            audioTrack = null;
        }

        if (fileInputStream != null) {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                Log.e(TAG, "Error closing file input stream.", e);
            }
            fileInputStream = null;
        }
    }
}

